package com.hellogood.yaohuo.server.mgr;

import java.util.List;
import java.util.Map;

import com.hellogood.yaohuo.server.DateUtil;
import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.shared.entity.Group;
import com.hellogood.yaohuo.shared.entity.Groupqx;
import com.hellogood.yaohuo.shared.entity.Log;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class GroupqxMgr {
	public List<Groupqx> getAllGroupqx() {
		return DAOFactory.getInstance().createGroupqxDAO().getAllGroupqx();
	}

	public String addGroupqx(List<Groupqx> qxs) {
		String result = DAOFactory.getInstance().createGroupqxDAO().addGroupqx(qxs);
		if (result.equals("")) {
			int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
			logID++;
			Log log = new Log();
			log.setLogId(logID);
			log.setStaffCode(MainFrame.currentUser.getStaffCode());
			log.setStaffName(MainFrame.currentUser.getStaffName());
			log.setLogDate(DateUtil.currentTime());
			log.setContent("设置分组权限");
			DAOFactory.getInstance().createLogDAO().addNewLog(log);
		}
		return result;
	}

	public String[][] buildGroupQx() {
		List<Group> groups = DAOFactory.getInstance().createGroupDAO().getAllGroup();
		Map<String, List<String>> map = DAOFactory.getInstance().createGroupqxDAO().getGroupqxMap();
		String[] mks = Groupqx.mks;
		String[][] objs = new String[groups.size()][mks.length];
		for (int i = 0; i < groups.size(); i++) {
			Group g = groups.get(i);
			objs[i][0] = g.getGroupCode();
			objs[i][1] = g.getGroupName();
			for (int j = 2; j < mks.length; j++) {
				String mk = mks[j];
				List<String> list = map.get(g.getGroupCode());
				if (list != null && list.contains(mk)) {
					objs[i][j] = "true";
				} else {
					objs[i][j] = "false";
				}
			}
		}
		return objs;
	}
	
	public List<Groupqx> getGroupQxListByUsercode(String code) {
		return DAOFactory.getInstance().createGroupqxDAO().getGroupQxListByUsercode(code);
	}
}
